//
//  YLRegisterViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/21.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRegisterViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "NSString+Regex.h"
#import "NSString+Hash.h"
#import "YLAccountTool.h"

@implementation YLRegisterViewModel

- (instancetype)init
{
    if (self = [super init]) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    _registerEnableSignal = [RACSignal combineLatest:@[RACObserve(self, phoneNumber), RACObserve(self, password), RACObserve(self, verifyCode), RACObserve(self, workerNumber)] reduce:^id _Nullable(NSString *phoneNumber, NSString *password, NSString *verifyCode, NSString *workerNumber){
        return @(phoneNumber.length && password.length && verifyCode.length && workerNumber.length);
    }];
    
    
    _registerCommend = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
       return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
           
           [WHProgressHUD showStatus:@"正在注册..."];
           
           NSString *url = [NSString stringWithFormat:@"%@Account/RegAndLogin", YLURL];
           
           // 判断密码
           {
               NSString *message = [NSString isOrNoPasswordStyle:weakSelf.password];
               if (message.length != 0) {
                   [WHProgressHUD showError:message];
                   [subscriber sendCompleted];
                   return nil;
               }
           }
           
           // 注册
           {
           NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:4];
           params[@"Tel"] = weakSelf.phoneNumber;
           params[@"UserCode"] = weakSelf.workerNumber;
           params[@"PassWord"] = [weakSelf.password md5String];
           params[@"VerifyCode"] = weakSelf.verifyCode;
           
           [[WHNetworkTool shareInstance] sendPostWithUrl:url params:params success:^(id responseObject) {
               
                   if ([responseObject[@"httpCode"] integerValue] == 200) {
                       YLAccount *account = [YLAccount accountWithDict:responseObject[@"httpResult"]];
                       account.phoneNumber = weakSelf.phoneNumber;
                       [YLAccountTool saveAccount:account];
                       [subscriber sendNext:nil];
                   } else {
                       [WHProgressHUD showError:responseObject[@"message"]];
                   
               }
               [subscriber sendCompleted];
                
           } fail:^(NSError *error) {
               [WHProgressHUD checkNetPlease];
               [subscriber sendCompleted];
           }];
           }
           
           
           return nil;
       }];
    }];
    
}

- (NSAttributedString *)attributedStringWithString:(NSString *)text
{
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0f]}];
    
    
    [att addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, 8)];
    
    [att addAttribute:NSForegroundColorAttributeName value:YLMainColor range:NSMakeRange(8, att.length - 8)];

    return att;
}

@end
